<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
	:row-style="rowStyle"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            @click="handleAdd(false)"
            v-permission="permissionList.addBtn"
          >
            添 加
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="handleBatchSubmit"
            v-permission="permissionList.submitBtn"
            v-if="false"
          >
            提 交
          </el-button>
          <el-button type="primary" size="mini" @click="submitData">加入报表差错</el-button>
        </template>
        <template slot="menu" slot-scope="{ row }">
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="handleView(row)"
          >
            预 览
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            v-permission="permissionList.delBtn"
            v-if="row.submitStatus == '0'"
            @click="handleDel(row)"
          >
            删 除
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-permission="permissionList.editBtn"
            v-if="editIsVis(row)"
            @click="handleEdit(row)"
          >
            编 辑
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-permission="permissionList.editBtn"
            v-if="editIsViss(row)"
            @click="mergeEdit(row)"
          >
            编 辑
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-discover"
            @click="openPastRecords(row)"
          >
            查看历史记录
          </el-button>
        </template>
      </avue-crud>

      <open-dialog
        :DialogVisible.sync="previewDialogVisible"
        :title="title"
        :destroy-on-close="true"
        @opened="dialogOpend"
      >
        <avue-form ref="form" v-model="form" :option="option" v-if="previewDialogVisible"></avue-form>
        <el-tabs v-model="activeTab" type="card" v-if="previewDialogVisible">
          <el-tab-pane label="故障退款" name="refundWork">
            <refund-work
              ref="refundWork"
              :workInfo="form"
              :boxType="boxType"
            ></refund-work>
          </el-tab-pane>
          <el-tab-pane label="付费出站发售" name="payWork">
            <pay-work
              ref="payWork"
              :workInfo="form"
              :boxType="boxType"
            ></pay-work>
          </el-tab-pane>
          <el-tab-pane label="现金事务" name="cashWork">
            <preview-cash-work
              ref="cashWork"
              :workInfo="form"
              :boxType="boxType"
            ></preview-cash-work>
          </el-tab-pane>
          <el-tab-pane label="电子事务" name="electronWork">
            <electron-work
              ref="electronWork"
              :workInfo="form"
              :boxType="boxType"
            ></electron-work>
          </el-tab-pane>
          <el-tab-pane label="特殊车票发售" name="specialWork">
            <special-work
              ref="specialWork"
              :workInfo="form"
              :boxType="boxType"
            ></special-work>
          </el-tab-pane>
          <el-tab-pane label="免费出站发售" name="freeWork">
            <free-work
              ref="freeWork"
              :workInfo="form"
              :boxType="boxType"
            ></free-work>
          </el-tab-pane>
        </el-tabs>
      </open-dialog>

      <open-dialog
        :DialogVisible.sync="dialogVisible"
        :title="title"
        :destroy-on-close="true"
        @opened="dialogOpend"
      >
        <avue-form ref="form" v-model="form" :option="option" v-if="dialogVisible"></avue-form>
        <el-tabs v-model="activeTab" type="card" v-if="dialogVisible">
          <el-tab-pane label="故障退款" name="refundWork">
            <refund-work
              ref="refundWork"
              :workInfo="form"
              :boxType="boxType"
            ></refund-work>
          </el-tab-pane>
          <el-tab-pane label="付费出站发售" name="payWork">
            <pay-work
              ref="payWork"
              :workInfo="form"
              :boxType="boxType"
            ></pay-work>
          </el-tab-pane>
          <el-tab-pane label="现金事务" name="cashWork">
            <cash-work
              ref="cashWork"
              :workInfo="form"
              :boxType="boxType"
            ></cash-work>
          </el-tab-pane>
          <el-tab-pane label="电子事务" name="electronWork">
            <electron-work
              ref="electronWork"
              :workInfo="form"
              :boxType="boxType"
            ></electron-work>
          </el-tab-pane>
          <el-tab-pane label="特殊车票发售" name="specialWork">
            <special-work
              ref="specialWork"
              :workInfo="form"
              :boxType="boxType"
            ></special-work>
          </el-tab-pane>
          <el-tab-pane label="免费出站发售" name="freeWork">
            <free-work
              ref="freeWork"
              :workInfo="form"
              :boxType="boxType"
            ></free-work>
          </el-tab-pane>
        </el-tabs>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="mergeDialogVisible"
        :title="title"
        :destroy-on-close="true"
        @opened="dialogOpend"
      >
        <avue-form ref="form" v-model="mergeEdiform" :option="mergeEditoption" v-if="mergeDialogVisible"></avue-form>
        <el-tabs v-model="activeTab" type="card" v-if="mergeDialogVisible">
          <el-tab-pane label="故障退款" name="refundWork">
            <refund-work
              ref="refundWork"
              :workInfo="mergeEdiform"
              :boxType="boxType"
            ></refund-work>
          </el-tab-pane>
          <el-tab-pane label="付费出站发售" name="payWork">
            <pay-work
              ref="payWork"
              :workInfo="mergeEdiform"
              :boxType="boxType"
            ></pay-work>
          </el-tab-pane>
          <el-tab-pane label="现金事务" name="cashWork">
            <cash-work
              ref="cashWork"
              :workInfo="mergeEdiform"
              :boxType="boxType"
            ></cash-work>
          </el-tab-pane>
          <el-tab-pane label="电子事务" name="electronWork">
            <electron-work
              ref="electronWork"
              :workInfo="mergeEdiform"
              :boxType="boxType"
            ></electron-work>
          </el-tab-pane>
          <el-tab-pane label="特殊车票发售" name="specialWork">
            <special-work
              ref="specialWork"
              :workInfo="mergeEdiform"
              :boxType="boxType"
            ></special-work>
          </el-tab-pane>
          <el-tab-pane label="免费出站发售" name="freeWork">
            <free-work
              ref="freeWork"
              :workInfo="mergeEdiform"
              :boxType="boxType"
            ></free-work>
          </el-tab-pane>
        </el-tabs>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="visableDialogRecords"
        title="查看历史记录"
        :destroy-on-close="true"
        @opened="dialogOpendRecords"
      >
        <avue-form ref="form" v-model="formRecords" :option="option" v-if="visableDialogRecords"></avue-form>
        <el-tabs v-model="activeTabRecords" type="card" v-if="visableDialogRecords">
          <el-tab-pane label="故障退款" name="refundWorkRecords">
            <refund-work-records
              ref="refundWorkRecords"
              :workInfoRecords="formRecordsDetail"
              :boxType="boxType"
            ></refund-work-records>
          </el-tab-pane>
          <el-tab-pane label="付费出站发售" name="payWorkRecords">
            <pay-work-records
              ref="payWorkRecords"
              :workInfoRecords="formRecordsDetail"
              :boxType="boxType"
            ></pay-work-records>
          </el-tab-pane>
          <el-tab-pane label="现金事务" name="cashWorkRecords">
            <cash-work-records
              ref="cashWorkRecords"
              :workInfoRecords="formRecordsDetail"
              :boxType="boxType"
            ></cash-work-records>
          </el-tab-pane>
          <el-tab-pane label="电子事务" name="electronWorkRecords">
            <electron-work-records
              ref="electronWorkRecords"
              :workInfoRecords="formRecordsDetail"
              :boxType="boxType"
            ></electron-work-records>
          </el-tab-pane>
          <el-tab-pane label="特殊车票发售" name="specialWorkRecords">
            <special-work-records
              ref="specialWorkRecords"
              :workInfoRecords="formRecordsDetail"
              :boxType="boxType"
            ></special-work-records>
          </el-tab-pane>
          <el-tab-pane label="免费出站发售" name="freeWorkRecords">
            <free-work-records
              ref="freeWorkRecords"
              :workInfoRecords="formRecordsDetail"
              :boxType="boxType"
            ></free-work-records>
          </el-tab-pane>
        </el-tabs>
      </open-dialog>
    </basic-container>
    <addReportEditor ref="addReportEditor" @addReportSuccess="getList"></addReportEditor>
  </div>
</template>

<script>
import baseCrud from "@/mixins/base-crud";
import { fetchList,addObj,delObj,queryPastRecords } from "@/api/subwaystm/swstpassengerwork";
import refundWork from "./refund-work";
import refundWorkRecords from "./refund-work-records";
import payWork from "./pay-work";
import payWorkRecords from "./pay-work-records";
import cashWork from "./cash-work";
import cashWorkRecords from "./cash-work-records";
import electronWork from "./electron-work";
import electronWorkRecords from "./electron-work-records";
import specialWork from "./special-work";
import specialWorkRecords from "./special-work-records";
import freeWork from "./free-work";
import freeWorkRecords from "./free-work-records";
import previewCashWork from "./preview-cash-work";
import { exportExcel } from "@/api/subwaystm/swstconductorsettle";
import { dateFormat } from "@/util/date";
import transitUtil from "@/api/subwayRat/transitUtil";
import addReportEditor from '@/views/stationTiecketManager/xianWngCJXXQR/BBCCGG/BBCCGGSQ/Editor';
export default {
  name: "swstpassengerwork",
  mixins: [baseCrud("subwaystm/swstpassengerwork")],
  provide() {
    return {
      passengerWork: this
    };
  },
  data() {
    return {
      sonId: 0,
      formRecords: {},
      formRecordsDetail: {},
      formSpecial: [],
      visableDialogRecords: false,
      activeTab: "refundWork",
      activeTabRecords: "refundWorkRecords",
      previewDialogVisible: false,
      dialogVisible: false,
      title: "",
      boxType: "",
      searchForm:{
        submitStatus:'0'
      },
      mergeDialogVisible: false,
      mergeEdiform: {},
      selectKeys:[],
      option: {
        menuBtn: false,
        labelWidth: 130,
        disabled: false,
        column: [
          {
            label: "售票员",
            prop: "conductorName",
            type: "select",
            component: "user-select",
            params: {
              belongToStation: true,
              userCodeKey: "conductorCode",
              operateNo: "deviceOperateNum"
              // TODO 过滤售票员
            },
            rules: [
              {
                required: true,
                message: "请选择售票员"
              }
            ],
            editDisabled: true
          },
          {
            label: "员工号",
            prop: "conductorCode",
            type: "input",
            placeholder: " ",
            disabled: true
          },
          {
            label: "收入日期",
            prop: "workDate",
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            rules: [
              {
                required: true,
                message: "请选择收入日期"
              }
            ],
            editDisabled: true
          },
          {
            label: "班次",
            prop: "classNumber",
            type: "select",
            dicUrl: "/admin/dict/type/stm_conductor_shift",
            rules: [
              {
                required: true,
                message: "请选择班次"
              }
            ],
            editDisabled: true
          },{
            type: "select",
            label: "设备操作编号",
            prop: "deviceOperateNum",
            search: "true",
            searchLabelWidth: 100,
            dicUrl: "/support/swpmdeviceoperationcode/page",
            props: {
              label: "operationCode",
              value: "operationCode",
              res: "data.records"
            },
            filterable: true,
            allowCreate: true,
            dicQuery: {
              size: -1
              //TODO 过滤售票员
            },
            rules: [
              {
                required: true,
                message: "请选择设备操作编号"
              }
            ]
          },{
            type: "select",
            label: "BOM编号",
            prop: "bomCode",
            rules: [
              {
                required: true,
                message: "请输入BOM编号"
              }
            ],
            filterable: true,
            dicUrl: "/admin/dict/type/bom_code",
            multiple: true
          }
        ]
      },
      mergeEditoption: {
        menuBtn: false,
        labelWidth: 130,
        disabled: false,
        column: [
          {
            label: "售票员",
            prop: "conductorName",
            type: "input",
            rules: [
              {
                required: true,
                message: "请选择售票员"
              }
            ],
            editDisabled: true
          },
          {
            label: "员工号",
            prop: "conductorCode",
            type: "input",
            disabled: true
          },
          {
            label: "收入日期",
            prop: "workDate",
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            rules: [
              {
                required: true,
                message: "请选择收入日期"
              }
            ],
            editDisabled: true
          },
          {
            label: "班次",
            prop: "classNumber",
            type: "select",
            dicUrl: "/admin/dict/type/stm_conductor_shift",
            rules: [
              {
                required: true,
                message: "请选择班次"
              }
            ],
            editDisabled: true
          },{
            type: "select",
            label: "设备操作编号",
            prop: "deviceOperateNum",
            search: "true",
            searchLabelWidth: 100,
            dicUrl: "/support/swpmdeviceoperationcode/page",
            props: {
              label: "operationCode",
              value: "operationCode",
              res: "data.records"
            },
            filterable: true,
            allowCreate: true,
            dicQuery: {
              size: -1
              //TODO 过滤售票员
            },
            rules: [
              {
                required: true,
                message: "请选择设备操作编号"
              }
            ]
          },{
            type: "select",
            label: "BOM编号",
            prop: "bomCode",
            rules: [
              {
                required: true,
                message: "请输入BOM编号"
              }
            ],
            filterable: true,
            dicUrl: "/admin/dict/type/bom_code",
            multiple: true
          }
        ]
      }
    };
  },
  created() {
    this.otherVueAction();
  },
  mounted() {
    let query = this.$route.query;
    if (query.dialogFlag == '0') {
      if(query.isMerge == '0'){
        let rows = query.datas;
        this.handleEdit(rows);
      }else{
        let rows = query.datas;
        this.mergeEdit(rows);
      }
    }else if(query.dialogFlag == '1'){
      this.form = query;
      this.handleConductorAdd(true);
    }
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.stm_sw_st_input_passenger_work_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.stm_sw_st_input_passenger_work_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.stm_sw_st_input_passenger_work_edit,
          false
        ),
        submitBtn: this.vaildData(
          this.permissions.stm_sw_st_input_passenger_work_submit,
          false
        )
      };
    }
  },
  components: {
    refundWork,
    refundWorkRecords,
    payWork,
    payWorkRecords,
    cashWork,
    cashWorkRecords,
    electronWork,
    electronWorkRecords,
    previewCashWork,
    specialWork,
    specialWorkRecords,
    freeWork,
    freeWorkRecords,
    addReportEditor
  },
  watch: {
    activeTab(newValue, oldValue) {
      this.initTabData();
    },
    activeTabRecords(newValue, oldValue) {
      this.initTabDataRecords();
    }
  },
  methods: {
     //颜色特殊标识
    rowStyle({ row, column, rowIndex }) {
       if (row.confirmStatus == "0") {
		return {
		  backgroundColor: "#eee",
		  color: "blue",
		};
          }
     
    },
    editIsVis(row) {
      if (row.isMerge == '1') {
        return false;
      } else {
        if (row.submitStatus == 1) {
          return false;
        } else {
          return true;
        }
      }
    },
    editIsViss(row) {
      if (row.isMerge == '0') {
        return false;
      } else {
        if (row.submitStatus == 1) {
          return false;
        } else {
          return true;
        }
      }
    },
    async submitData() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      if (this.$refs.crud.tableSelect.length != 1) {
        this.$message.error("只能选择一条数据");
        return false;
      }
      let res = false;
      let ress = false;
      for (const table of this.$refs.crud.tableSelect) {
        if (table.submitStatus != "1") {
          this.$message.error("请选择已提交的数据");
          return false;
        }else{
          if (table.isAddReprotError == "1") {
            res = await this.$confirm('该数据已加入报表差错，是否重新加入？', '提示', {
              confirmButtonText: '是',
              cancelButtonText: '否',
              type: 'warning'
            });
            if (!res) {
              return;
            }else{
              ress = true;
            }
          }
        }
      }
      if(res==false){
        ress = await this.$confirm("是否加入报表差错", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        });
      }
      if (!ress) {
        return;
      }
      this.selectKeys = this.$refs.crud.tableSelect;
      let param={
        dialogTitle: "加入报表差错",
        diffAmountId:this.selectKeys[0].id,
        dataSourceTable:"sw_st_input_passenger_work",
        lineCode:this.selectKeys[0].lineCode,
        stationCode:this.selectKeys[0].stationCode,
        bomCode:this.selectKeys[0].bomCode,
        reprotTime:this.selectKeys[0].workDate,
        reportNameId:2,
        deviceNo:this.selectKeys[0].deviceOperateNum,
        settleBeginTime:this.selectKeys[0].workDate,
        settleEndTime:this.selectKeys[0].workDate,
        userCodes:this.selectKeys[0].conductorCode,
	dutyStationLeaderCode: this.selectKeys[0].entryUpdateJobNo, 
	dutyStationCode: this.selectKeys[0].entryUpdateJobName,
        showType:3,
        dataSource:1,
        page:this.page
      };
      this.$refs.addReportEditor.others_report_add_call(param);
    },
    async queryPastRecords(row){
      let param={};
      param.id=row.id;
      await queryPastRecords(param)
        .then(response => {
          this.formRecords = response.data.data;
          this.formRecordsDetail = response.data.data;
        })
    },
    openPastRecords(row){
      this.formRecords = {};
      this.formRecordsDetail = {};
      this.queryPastRecords(row);
      this.boxType = "records";
      //this.formRecords = row;
      this.option.disabled = true;
      this.visableDialogRecords=true;
    },
    handleDel(row) {
      if (row.reportModifyStatus == "1") {
        this.$message.error("报表修改状态为“是”的情况下，不允许删除数据");
        return false;
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delObj(row.id)
          .then((data) => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
    validateForm(callback) {
      return this.$refs.form.validate(callback);
    },
    handleConductorAdd(disabled = false) {
      this.title = "添加";
      this.boxType = "add";
      this.option.disabled = disabled;
      this.dialogVisible = true;
    },
    handleAdd(disabled = false) {
      this.title = "添加";
      this.boxType = "add";
      this.option.disabled = disabled;
      let now = new Date();
      if (now.getHours() < 6) {
        now.setDate(now.getDate() - 1);
      }
      this.form.workDate = dateFormat(now).substring(0, 10);
      this.dialogVisible = true;
    },
    // handleExport(){

    // },
    handleView(row) {
      this.title = "预览";
      this.boxType = "view";
      this.form = row;
      this.option.disabled = true;
      this.previewDialogVisible = true;
    },
    handleEdit(row) {
      this.title = "编辑";
      this.boxType = "edit";
      this.form = row;
      this.option.disabled = true;
      this.dialogVisible = true;
    },
    mergeEdit(row){
      this.title = "合并编辑";
      this.boxType = "mergeEdit";
      this.mergeEdiform = row;
      this.mergeEditoption.disabled = true;
      this.mergeDialogVisible = true;
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    closeDialogPastRecords() {
      this.visableDialogRecords=false;
    },
    closeDialogMerge() {
      this.mergeDialogVisible = false;
    },
    dialogOpend() {
      this.activeTab = 'refundWork'
      this.initTabData();
    },
    dialogOpendRecords() {
      this.activeTabRecords = 'refundWorkRecords'
      this.initTabDataRecords();
    },
    initTabData() {
      if (this.boxType == "add") {
        // this.$refs[this.activeTab].clear()
      } else {
        this.$refs[this.activeTab].getList();
      }
    },
    initTabDataRecords() {
      if(this.boxType == "records"){
        this.$refs[this.activeTabRecords].getList();
      }
    },
    dialogClose() {
      this.form = {
        conductorName: "",
        conductorCode: "",
        workDate: "",
        classNumber: "",
        deviceOperateNum: "",
        bomCode: ""
      };
      if (this.boxType == "add") {
        this.getList(this.page);
      }
    },
    otherVueAction(){
      let th=this;
      transitUtil.$on('updateReport_call_2',(param)=>{
        let p=param;
        fetchList(p).then(response => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
      });
    }
  }
};
</script>
